package com.tensquare.user.controller;

import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.user.pojo.User;
import com.tensquare.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

  @Autowired private UserService userService;

  @RequestMapping(value = "/login", method = RequestMethod.POST)
  public Result login(@RequestBody User user) {
    User result = userService.login(user);
    if (result != null) {
      return new Result(true, StatusCode.OK, "登陆成功", result);
    }
    return new Result(false, StatusCode.LOGINERROR, "登陆失败");
  }

  /**
   * 根据ID查询用户
   *
   * @param id
   * @return
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public Result selectById(@PathVariable String id) {
    User user = userService.selectById(id);
    return new Result(true, StatusCode.OK, "查询成功", user);
  }
}
